<?php

if(!defined('IN_NETGORESP')){ exit; }

class cookie extends db
{
	/*
		Cookies Are Awesome!
	*/
	function parse_cookie($db, $template) 
	{
	  $row = false;
	    
    if (isset($_COOKIE["netgoresp"])) 
    {
  		$cookieinfo = explode(" ",$_COOKIE["netgoresp"]);
  	  $query = $db->sql_query("SELECT * FROM {{table}} WHERE name='$cookieinfo[0]'", "account");
  		
      if ($db->sql_rows($query) != 1) 
				die($template->error("1",	"Invalid cookie data. Please clear cookies and log in again.")); 
			else 
			  $row = $db->sql_array($query);
			
			if ($row["name"] != $cookieinfo[0]) 
				die($template->error("2", "Invalid cookie data. Please clear cookies and log in again."));   
			
			if (md5($row["password"]) != $cookieinfo[1]) 
				die($template->error("3", "Invalid cookie data. Please clear cookies and log in again.")); 
			
      // If we've gotten this far, cookie should be valid, so write a new one.
      $newcookie = implode(" ",$cookieinfo);
			
			if ($cookieinfo[2] == 1) 
				$exptime = time()+31536000; 
			else 
				$exptime = 0; 
				
			setcookie("netgoresp", $newcookie, $exptime, "/", "", 0);      
     }
	        
	   return $row;
	}
	
	/*
	-------------------------------------------------------------
	Cookie_Error Template
	-------------------------------------------------------------
	@param	int		$errno		Contains the Error Number
	@param	string	$reason		Contains the Base Error
	-------------------------------------------------------------
	*/
	function cookie_error($errno,$reason)
	{
	return '<html>
				<head>
					<title>NetgoreSP - Cookie Error #'.$errno.'</title>
					<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
					<link rel="stylesheet" type="text/css" href="template/vbgoresp/css/template_error.css" />
				</head>
				<body>
					<div id="container">
						<h1 style="border-bottom: 1px solid #ddd; margin-bottom: 4px;">Netgore<span style="color:limegreen">SP</span> &rarr; Cookie Error #'.$errno.'</h1><br />
						<div class="error">'.$reason.'</div><br />
						<div class="note"><font style="color:#990000;">*Note:</font> If you cannot figure out how to get rid of this message or do not understand the error, please message "DarkGrave" on http://www.netgore.com/. Thank you very much.</div>
				</body>
			</html>';
	}
}

?>